package com.sunday.electric.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.MediaController;

import com.sunday.common.event.EventBus;
import com.sunday.electric.R;
import com.sunday.electric.adapter.RecentAdapter;
import com.sunday.electric.base.BaseActivity;
import com.sunday.electric.base.BaseApplication;
import com.sunday.electric.db.RecentDB;
import com.sunday.electric.entity.RecentItem;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by wang on 2016/4/3.
 */
public class RecentChatActivity extends BaseActivity {
    @Bind(R.id.list_view)
    ListView listView;
    private RecentDB mRecentDB;
    private List<RecentItem> dataSet;
    private RecentAdapter recentAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recent_chat);
        ButterKnife.bind(this);
        EventBus.getDefault().post(this);
        mRecentDB = BaseApplication.getInstance().getRecentDB();// 接收消息数据库
        dataSet = mRecentDB.getRecentList();
        recentAdapter = new RecentAdapter(mContext,dataSet,R.layout.list_recent_item);
        listView.setAdapter(recentAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RecentItem item = dataSet.get(position);
                intent = new Intent(mContext,OnLineHelpActivity.class);
                intent.putExtra("loginName",item.getComeFrom());
                startActivity(intent);
            }
        });
    }

    public void onEvent(RecentItem recentItem){
        boolean flag = false ;
        for(int i=0;i<dataSet.size();i++){
            if(recentItem.getUserId().equals(recentItem.getUserId())){
                dataSet.set(i,recentItem);
                flag = true;
                break;
            }
        }
        if(!flag){
            dataSet.add(0,recentItem);
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                recentAdapter.notifyDataSetChanged();
            }
        });
    }
}
